package edu.utfpr.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Images.Media;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;

public class ViewEditor extends Activity {

	// variaveis
	private CanvasView view;
	private Canvas canvas;
	private Bitmap image;
	private Bundle extras;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		carregaEditor();
	}

	public void carregaEditor() {

		try {
			extras = getIntent().getExtras();
			image = Media.getBitmap(getContentResolver(),
					Uri.parse(extras.getString("imageUri")));
			view = new CanvasView(getApplicationContext());
			setContentView(view);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// classe para criar a view e editar as imagens
	public class CanvasView extends SurfaceView implements SurfaceHolder.Callback {

		// variaveis
		private CanvasThread thread;
		public Paint paint;

		public CanvasView(Context context) {
			super(context);
			getHolder().addCallback(this);
			paint = new Paint(Paint.FILTER_BITMAP_FLAG);
			thread = new CanvasThread(getHolder(), this, paint);
		}

		public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		}

		public void surfaceCreated(SurfaceHolder holder) {
			thread.inicializar();
			thread.setRunning(true);
			thread.start();
		}

		public void surfaceDestroyed(SurfaceHolder holder) {
			boolean retry = true;
			thread.setRunning(false);
			while(retry){
				try{
					thread.join();
					retry = false;
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			try{
				thread.setEvent(event);
				return true;
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
		}
	}

	@SuppressLint("ShowToast")
	public class CanvasThread extends Thread{
		
		private SurfaceHolder surfaceholder;
		private CanvasView canvasview;
		private Paint paint;
		private boolean run = false;
		private MotionEvent event = null;
		private int xlast, ylast, xnow, ynow;
		
		public CanvasThread(SurfaceHolder holder, CanvasView view, Paint paint){
			this.paint = paint;
			this.paint.setColor(Color.RED);
			this.paint.setStrokeWidth(4);
			this.paint.setStrokeCap(Paint.Cap.ROUND);
			this.paint.setStrokeJoin(Paint.Join.ROUND);
			surfaceholder = holder;
			canvasview = view;
		}
		
		public void setRunning(boolean run){
			this.run = run;
		}
		
		public void setEvent(MotionEvent event){
			this.event = event;
		}
		
		public void inicializar(){
			try {
				canvas = surfaceholder.lockCanvas(null);
				synchronized (surfaceholder) {
					canvas.drawBitmap(image, null, new Rect(0, 0, canvasview.getWidth(),canvasview.getHeight()), paint);
				}
			} finally {
				if (canvas != null) {
					surfaceholder.unlockCanvasAndPost(canvas);
				}
			}
		}

		public void desenhar(MotionEvent event) {

			switch (event.getAction() & event.getActionMasked()) {

			case MotionEvent.ACTION_DOWN:
				xlast = (int) event.getX();
				ylast = (int) event.getY();
				break;

			case MotionEvent.ACTION_MOVE:
				break;

			case MotionEvent.ACTION_UP:
				try {
					canvas = surfaceholder.lockCanvas(null);
					synchronized (surfaceholder) {
						xnow = (int) event.getX();
						ynow = (int) event.getY();
						canvas.drawLine(xlast, ylast, xnow, ynow, paint);
						xlast = xnow;
						ylast = xnow;
					}
				} finally {
					event = null;
					if (canvas != null) {
						surfaceholder.unlockCanvasAndPost(canvas);
					}
				}
				break;

			case MotionEvent.ACTION_POINTER_DOWN:
				break;

			case MotionEvent.ACTION_POINTER_UP:
				break;
			}
		}
		
		@Override
		public void run() {
			while (run) {
				if (event != null) {
					desenhar(event);
					event = null;
				}
			}
		}
	}
}

